package com.lkl.springcloud.controller;

import com.lkl.springcloud.pojo.Dept;
import com.lkl.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//提供Restful服务(接口)！
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/get/{id}")
    @HystrixCommand(fallbackMethod = "hystrixGet") //如果根据id查询失败，就会启用备选方案
    public Dept get(@PathVariable("id") Long id) {
        Dept dept = deptService.queryById(id);

        if(dept==null){
            throw new RuntimeException("id->"+id+",请输入正确的部门id，信息无法找到");
        }
        return dept;
    }

    //根据id查询备选方案(熔断)
    public Dept hystrixGet(@PathVariable("id") Long id) {
        return new Dept()
                .setDeptno(id)
                .setDname("id->"+id+",信息无法找到--@hystrix")
                .setDb_source("no this database in Mysql");
    }

}
